/*
package com.example.rumen;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class PictureInternet extends AppCompatActivity {

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            if (msg.what == 0x01) {
                image_view.setImageBitmap(bitmap1);
            }
        }
    };

    private  int currentIndex = 0;
    private ImageView image_view;
    private Bitmap bitmap1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_picture_internet);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        image_view = findViewById(R.id.imageView0408);
        //开辟子线程到网络上获取图片数据流

        new Thread() {
            @Override
            public void run() {
                super.run();

                try {
                    String linkUrl1 = "https://img0.baidu.com/it/u=3948054425,3928451467&fm=253&fmt=auto?w=800&h=1422";
                    String linkUrl2= "https://https://img2.baidu.com/it/u=420754388,3769109030&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1427mg0.baidu.com/it/u=4264397210,3742258030&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=1730";
                    String linkUrl3 = "https://img0.baidu.com/it/u=4109649721,4033017417&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=889";
                    String linkUrl4 = "https://img1.baidu.com/it/u=3876643274,3314067889&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=879";
                    String linkUrl5 = "https://img2.baidu.com/it/u=3411981163,668581563&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1733";
                    String []LinkURL = new String[]{linkUrl1,linkUrl2,linkUrl3,linkUrl4,linkUrl5};
                    if (currentIndex==4)currentIndex=0;
                    URL url = new URL(LinkURL[currentIndex++]);

                    try {
                        URLConnection urlConnection = url.openConnection();
                        InputStream inputStream = urlConnection.getInputStream();
                        bitmap1 = BitmapFactory.decodeStream(inputStream);
                        handler.sendEmptyMessage(0x01);//不用创建Message对象，直接发送一个整型值
                        Thread.sleep(5000);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }

                } catch (MalformedURLException e) {
                    throw new RuntimeException(e);
                }

            }
        }.start();

    }
}*/



package com.example.rumen;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class PictureInternet extends AppCompatActivity {

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            if (msg.what == 0x01) {
                image_view.setImageBitmap(bitmap1);
                // 设置一个延迟，5秒后加载下一张图片
                handler.sendEmptyMessageDelayed(0x02, 5000);
            } else if (msg.what == 0x02) {
                // 收到延迟消息后，加载下一张图片
                loadNextImage();
            }
        }
    };

    private int currentIndex = 0;
    private ImageView image_view;
    private Bitmap bitmap1;
    private String[] imageUrls;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_picture_internet);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        image_view = findViewById(R.id.imageView0408);

        // 初始化图片URLs
        imageUrls = new String[]{
                "https://img0.baidu.com/it/u=3948054425,3928451467&fm=253&fmt=auto?w=800&h=1422",
                "https://img2.baidu.com/it/u=420754388,3769109030&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1427",
                "https://img0.baidu.com/it/u=4109649721,4033017417&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=889",
                "https://img1.baidu.com/it/u=3876643274,3314067889&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=879",
                "https://img2.baidu.com/it/u=3411981163,668581563&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1733"
        };

        // 开始加载第一张图片
        loadNextImage();
    }

    // 加载下一张图片的方法
    private void loadNextImage() {
        // 启动新线程加载图片
        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    // 使用当前索引获取URL并增加索引
                    URL url = new URL(imageUrls[currentIndex]);
                    // 更新索引，确保循环
                    currentIndex = (currentIndex + 1) % imageUrls.length;

                    try {
                        URLConnection urlConnection = url.openConnection();
                        InputStream inputStream = urlConnection.getInputStream();
                        bitmap1 = BitmapFactory.decodeStream(inputStream);
                        inputStream.close(); // 关闭输入流

                        // 发送消息到主线程更新UI
                        handler.sendEmptyMessage(0x01);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 移除所有消息，防止内存泄漏
        handler.removeCallbacksAndMessages(null);
    }
}